<template>
  <view class="navigation">
    <view class="left">
      <view class="item">
        <u-icon
          name="server-fill"
          :size="40"
          :color="$u.color['contentColor']"
        ></u-icon>
        <view class="text u-line-1">客服</view>
      </view>
      <!-- <view class="item">
        <u-icon
          name="home"
          :size="40"
          :color="$u.color['contentColor']"
        ></u-icon>
        <view class="text u-line-1">店铺</view>
      </view> -->
      <navigator url="/pages/cart/cart" open-type="switchTab" class="item car">
        <u-badge
          class="car-num"
          :count="goodsTotal"
          type="error"
          :offset="[-3, -6]"
        ></u-badge>
        <u-icon
          name="shopping-cart"
          :size="40"
          :color="$u.color['contentColor']"
        ></u-icon>
        <view class="text u-line-1">购物车</view>
      </navigator>
    </view>
    <view class="right">
      <view class="cart btn u-line-1" @click="addGoodCart">加入购物车</view>
      <view class="buy btn u-line-1">立即购买</view>
    </view>
  </view>
</template>

<script>
import { mapMutations, mapGetters } from 'vuex'
export default {
  props: {
    good: {
      type: Object,
      default: () => { }
    },
  },
  computed: {
    ...mapGetters('cart', ['goodsTotal']),
  },
  // 组件要使用 vuex生命周期
  //  小程序中 本地存储 存储任意类型数据 ，不像网页一样只能存json字符串
  created () {
    console.log('6666', this.$store.state.cart.goodList)
    console.log('总数', this.goodsTotal)
    // // 存
    // uni.setStorageSync('本地存储', [123, 555])
    // // 取
    // console.log(uni.getStorageSync('本地存储'))
  },
  methods: {
    ...mapMutations('cart', ['setGoodList']),
    addGoodCart () {
      // this.$store.commit('cart/setGoodList', { ...this.good, checked: true, count: 1 })
      this.setGoodList({
        ...this.good,
        checked: true,
        count: 1,
        // 添加 每项商品滑动打开状态
        show: false
      })
    }
  },
};
</script>

<style lang="scss" scoped>
.navigation {
  display: flex;
  margin-top: 100rpx;
  border: solid 2rpx #f2f2f2;
  background-color: #ffffff;
  padding: 16rpx 0;
  .left {
    display: flex;
    font-size: 20rpx;
    .item {
      margin: 0 30rpx;
      &.car {
        text-align: center;
        position: relative;
        margin-right: 90rpx;
        .car-num {
          position: absolute;
          top: -10rpx;
          right: -10rpx;
        }
      }
    }
  }
  .right {
    display: flex;
    font-size: 28rpx;
    align-items: center;
    .btn {
      line-height: 66rpx;
      padding: 0 30rpx;
      border-radius: 36rpx;
      color: #ffffff;
    }
    .cart {
      background-color: #ed3f14;
      margin-right: 30rpx;
    }
    .buy {
      background-color: #ff7900;
    }
  }
}
</style>
